#!/usr/bin/python3
import datetime
import time
import requests
import re
from bs4 import BeautifulSoup
from datetime import date
from peewee import *
UrlHost = "https://www.qinbing.cn"
# https://www.qinbing.cn/Portal/Index/news/page/2/type/357
btwaf = "000000"
yema = 1
listye = "/Portal/Index/news/page/{}/type/357"
headers = {
     # "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36",
     # "cookie": "55DMI1_think_language=zh-CN;  user=eyJwaG9uZSI6IjE3MTA2NjMyMjUyIiwicGFzc3dvcmQiOiJsaTE5OTkwNjEwIn0="
}
db = MySQLDatabase('jdjg', host='81.69.234.172', port=3306, user='jdjg', password='admin')

class Jg(Model):
    uptime = DateField( default=datetime.datetime.now())
    address = CharField(unique=True)
    jiage = FloatField(default=0.0)
    class Meta:
        database = db
db.connect()
db.create_tables([Jg])


def gethtml(url):
    global yema
    # print("gethtml")
    res = requests.get(url, headers=headers)
    # print(res.text) # <html><meta charset="utf-8" /><title>检测中</title><div>跳转中</div></html> <script> window.location.href ="/Portal/Index/news/page/1/type/357?btwaf=42356825"; </script>
    if "跳转" in res.text:
        print("需要跳转---",end="")
        start = res.text.find("?btwaf=")
        end = res.text.find('";', start)
        newUrl =url + res.text[start:end]
        print(newUrl)
        res = requests.get(newUrl, headers=headers)
    return res.text



#    >.*?-.*?鸡蛋价格.*?参考价.*?[0-9].[0*9].*?<
def qvjg(html):
    soup = BeautifulSoup(html, 'html.parser')
    tag = soup.select("div.news>div>table tr")
    for i in tag:
        print(i)
        if "鸡蛋价格" in str(i):
            ## x:str="">周口-太康 <a href="https://www.qinbing.cn/Portal/Index/news/type/357" target="_blank">鸡蛋价格</a>（大）： 参考价约</td><td x:num="5.43333333333333">5.43</td>
            # print(i)
            name = re.search("x:str=.*?<a..|x:str.*?>.*?</td>", str(i))
            num = re.search("num.*?[0-9].>", str(i))
            if name:
                address = (name.group()[9:-4].strip()).split(" ")[0]
                if num:
                    jg = float(num.group()[5:-2].strip())
                    print(name.group() , num.group(),"-----------")
                    try:
                        print("找-->",address,end="--------=-=-=-=----")
                        Jg.get(Jg.address == address)
                        rows = Jg.update(jiage=jg, uptime=date.today()).where(Jg.address == address).execute()
                        print("row" ,rows, "更新----{}，价格----{}".format(address, jg))
                        # 方法2 - 使用update更新 (方法1需要先查询在更新，方法二直接更新，所以更新操作推荐使用方法2)
                        # rows = User.update(age=29).where(User.username == "charlie").execute()
                    except:
                        Jg.create(address=address, jiage=jg, uptime=date.today())
                        print("添加{}，价格{}".format(address, jg))


def app(now):
    global yema
    while yema < 100:
        html = gethtml("https://www.qinbing.cn/Portal/Index/news/page/{}/type/357".format(yema))
        yema += 1
        soup = BeautifulSoup(html, 'html.parser')
        tag = soup.select("div.middle-left")
        print(tag)
        for i in tag:
            h1 = i.select_one("h1 > a")
            # print(h1)
            href = str(h1)[9:47]
            title = str(h1)[65:-4]
            print(title, UrlHost + href)
            if re.match(now,title):
                print("是今天")
                qvjg(gethtml(UrlHost + href))
                time.sleep(5)
            else:
                # print("不是今天  tiaoguo")
                yema = 100
                break



if __name__ == '__main__':
    now_time = str(date.today()).split("-")
    # print(now_time)
    now_str = now_time[0] + "年" + str(int(now_time[1])) + "月" + str(int(now_time[2])) + "日.*?价格行情.*?"
    # ma_str = "2022" + "年" + "10" + "月" + "11" + "日.*?价格行情.*?"
    print(now_str)
    app(now_str)